package com.bamanker.dailylove.utils;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.bamanker.dailylove.config.DailyLoveConfigure;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.Strings;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DataUtils {

    /**
     * 计算农历生日天数
     *
     * @param birthday
     * @return
     */
    public static int getChineseBirthdays(String birthday) {
        //获取输入的生日
        String boyBirthday = DailyLoveConfigure.Boy_Birthday;
        //获取农历生日
        ChineseDate chineseBir = new ChineseDate(DateUtil.parseDate(DailyLoveConfigure.Boy_Birthday));
        log.info("生日的农历日期是：{}", chineseBir);
        //截取日、月
        String[] strings = Strings.split(boyBirthday, '-');
        int chineseMonth = chineseBir.getMonth();
        int chineseDay = chineseBir.getDay();
        //获取当前日期的年
        Calendar dateToday = Calendar.getInstance();
        int todayYear = dateToday.get(Calendar.YEAR);
        //把生日的年改为今年，方便计算
        ChineseDate chineseDate = new ChineseDate(todayYear, chineseMonth, chineseDay,false);
        //农历日期对应的阳历日期
        int gregorianDay = chineseDate.getGregorianDay();
        //计算时间差
        long days = haveThisDay(chineseMonth, chineseDay, dateToday, todayYear, chineseDate, gregorianDay);
        log.info("days:{}", days);
        return (int) days;
    }

    /**
     * 需要递归计算日期差
     *
     * @param chineseMonth 农历月
     * @param chineseDay   农历日
     * @param dateToday    今天的日期类
     * @param todayYear    当前的年
     * @param chineseDate  组装的待计算的新日期
     * @param gregorianDay 判断是否存在农历日期的参数，-1代表今年不存在这个农历日期
     * @return 计算好的天数
     */
    private static long haveThisDay(int chineseMonth, int chineseDay, Calendar dateToday, int todayYear, ChineseDate chineseDate, int gregorianDay) {
        //判断当前年份是否存在农历日对应的阳历日
        while (gregorianDay == -1) {
            //不存在，计算明年
            todayYear += 1;
            chineseDate = new ChineseDate(todayYear, chineseMonth, chineseDay, false);
            gregorianDay = chineseDate.getGregorianDay();
        }
        //存在
        //将农历日期改为阳历日期
        Calendar gregorianbir = chineseDate.getGregorianCalendar();
        //判断这个日期是否和今年是同一年
        if (todayYear == dateToday.get(Calendar.YEAR)) {
            //是同一年，判断这一天过了没
            if (gregorianbir.get(Calendar.DAY_OF_YEAR) < dateToday.get(Calendar.DAY_OF_YEAR)) {
                //这一天过了，计算明年
                todayYear += 1;
                chineseDate = new ChineseDate(todayYear, chineseMonth, chineseDay, false);
                gregorianDay = chineseDate.getGregorianDay();
                //递归计算下一年是否存在对应的阳历日
                return haveThisDay(chineseMonth, chineseDay, dateToday, todayYear, chineseDate, gregorianDay);
            }
            //没有过，直接计算
        }
        //不是同一年，直接计算
        Date time = dateToday.getTime();
        log.info("当前的日期是：{}", time);
        Date time1 = gregorianbir.getTime();
        log.info("下一个生日的日期：{}", time1);
        return DateUtil.between(dateToday.getTime(), gregorianbir.getTime(), DateUnit.DAY);

    }

    /**
     * 计算生日天数 days
     *
     * @param birthday
     * @return
     */
    public static int getBirthdays(String birthday) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 存今天
        Calendar dateToday = Calendar.getInstance();
        // 存生日
        Calendar dateBirth = Calendar.getInstance();
        int days = 0;
        try {
            // 设置生日
            dateBirth.setTime(dateFormat.parse(birthday));
            // 修改为本年
            dateBirth.set(Calendar.YEAR, dateToday.get(Calendar.YEAR));
            if (dateBirth.get(Calendar.DAY_OF_YEAR) < dateToday.get(Calendar.DAY_OF_YEAR)) {
                // 生日已经过了，要算明年的了
                days = (dateToday.getActualMaximum(Calendar.DAY_OF_YEAR) - dateToday.get(Calendar.DAY_OF_YEAR))
                        + dateBirth.get(Calendar.DAY_OF_YEAR);
            } else {
                // 生日还没过
                days = dateBirth.get(Calendar.DAY_OF_YEAR) - dateToday.get(Calendar.DAY_OF_YEAR);

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 计算恋爱天数 days
     *
     * @param loveday
     * @return
     */
    public static int getLoveDays(String loveday) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        try {
            long time = System.currentTimeMillis() - dateFormat.parse(loveday).getTime();
            days = (int) (time / (24 * 60 * 60 * 1000));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }
}
